import { createApp, reactive } from "vue"
import App from "./App.vue"
import Antd from "ant-design-vue"
import "ant-design-vue/dist/reset.css"
import "@icon-park/vue-next/styles/index.css"
import "@/style.css"
import { Store } from "@tauri-apps/plugin-store"

const app = createApp(App)
Store.load("store.json")
const globalVars = reactive({
    panes: [],
    activeKey: "Search",
    changeItemId: "",
    addPane: (key, title, closable = true, addNew = false, data = {}) => {
        //addNew 为true时，强制添加新的pane,key是唯一的，用户复制当前pane时，会生成新的pane
        if (key !== "Tool" && key !== "Search" && !addNew && key != "api_item") {
            globalVars.activeKey = key
            console.log("添加", key)
        }
        if (addNew) {
            key = key + "_#_" + new Date().getTime()
            globalVars.activeKey = key
        } else {
            for (const pane of globalVars.panes) {
                if (pane.key === key) {
                    console.log("已存在", key)
                    return
                }
            }
        }
        globalVars.panes.push({ key, title, closable, data })
        localStorage.setItem("activeKey", JSON.stringify({ k: globalVars.activeKey, v: globalVars.panes }))
    },
    rename(key, title) {
        for (let i = 0; i < globalVars.panes.length; i++) {
            if (globalVars.panes[i].key === key) {
                globalVars.panes[i].title = title
                break
            }
        }
    },
    renameLike(key, title) {
        for (let i = 0; i < globalVars.panes.length; i++) {
            if (globalVars.panes[i].key.indexOf(key) !== -1) {
                globalVars.panes[i].title = title
            }
        }
    },
    changePane: (key) => {
        globalVars.activeKey = key
        console.log("切换", key)
        localStorage.setItem("activeKey", JSON.stringify({ k: globalVars.activeKey, v: globalVars.panes }))
    },
    removePane: (key) => {
        globalVars.panes = globalVars.panes.filter((pane) => pane.key !== key)
        if (globalVars.activeKey === key) {
            if (globalVars.panes.length > 0) {
                globalVars.activeKey = globalVars.panes[globalVars.panes.length - 1].key
            } else {
                globalVars.activeKey = "Search"
            }
            console.log("删除", key, "切换", globalVars.activeKey)
        } else {
            console.log("删除", key, "不切换")
        }
        localStorage.setItem("activeKey", JSON.stringify({ k: globalVars.activeKey, v: globalVars.panes }))
    },
    removePaneLike: (key) => {
        globalVars.panes = globalVars.panes.filter((pane) => pane.key.indexOf(key) === -1)
        if (globalVars.activeKey.indexOf(key) !== -1) {
            if (globalVars.panes.length > 0) {
                globalVars.activeKey = globalVars.panes[globalVars.panes.length - 1].key
            } else {
                globalVars.activeKey = "Search"
            }
            console.log("删除", key, "切换", globalVars.activeKey)
        } else {
            console.log("删除", key, "不切换")
        }
        localStorage.setItem("activeKey", JSON.stringify({ k: globalVars.activeKey, v: globalVars.panes }))
    },
})

const ak = localStorage.getItem("activeKey")
if (ak) {
    try {
        const d = JSON.parse(ak)
        if (d.v.length > 0) {
            globalVars.panes = d.v
        }
        if (d.k) {
            globalVars.activeKey = d.k
            console.log("恢复", d.k)
        }
    } catch (error) {}
}
// try {
//     let c = await Store.load("store.json")
//     console.log("111", await c.set("1", 1))

//     console.log("111", await c.get("1"))
// } catch (error) {
//     console.log("error", error)
// }
app.provide("globalVars", globalVars)
app.use(Antd).mount("#app")
